package com.king01.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.king01.dao.StaffDao;
import com.king01.entity.Branch;
import com.king01.entity.Department;
import com.king01.entity.Role;
import com.king01.entity.Staff;
import com.king01.service.StaffService;
import com.king01.vo.Result;
import com.king01.vo.StaffExVo;
import com.king01.vo.StaffVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author ：lqs
 * @date ：Created in 2023/9/25 16:05
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffDao staffDao;
    @Autowired
    private HttpSession session;
    @Override
    public Result findByNameAndPwd(String name, String pwd) {
        Staff staff = staffDao.selectByNameAndPwd(name, pwd);
        //账号密码错误
        if (staff==null){
            return new Result(500,"账号密码错误",null);
        }
        //表示被冻结
        if (staff.getState()==2){
            return new Result(500,"账号被冻结，请联系管理员",null);
        }
        //密码正确
        //把当前用户放进session
        session.setAttribute("staff",staff);
        return new Result(200,"登陆成功",null);

    }

    //查询员工信息
    public Result findAll(Integer current, Integer pageSize, StaffVo staffVo) {
        Staff staff = (Staff) session.getAttribute("staff");
        PageHelper.startPage(current, pageSize);
        if (!staff.getStaffid().equals(111)) {
            staffVo.setBranchid(staff.getBranchid());
        }
        List<Staff> list = staffDao.findAll(staffVo);
        PageInfo<Staff> pageInfo = new PageInfo<Staff>(list);
        return new Result(200, "查询成功", pageInfo);


    }

    @Override
    public Result update1(Integer staffid) {
        Integer integer = staffDao.update1(staffid);
        return new Result(200, "查询成功", integer);
    }

    @Override
    public Result update2(Staff staff) {
        Integer integer = staffDao.update2(staff);
        return new Result(200, "修改成功", integer);
    }

    @Override
    public Result select1() {
        List<Branch> list = staffDao.select1();
        return new Result(200, "修改成功", list);
    }

    @Override
    public Result select2() {
        List<Department> departments = staffDao.select2();
        return new Result(200, "修改成功", departments);
    }

    @Override
    public Result select3() {
        List<Role> roles = staffDao.select3();
        return new Result(200, "修改成功", roles);
    }

    @Override
    public Result add1(Staff staff) {
        Integer integer = staffDao.add1(staff);
        if (integer>0) {
            return new Result(200, "添加成功", integer);
        }else {
            return new Result(500, "添加失败", integer);

        }
    }

    @Override
    public Result selectIF(Integer staffid) {
        List<StaffExVo> staffExVos = staffDao.selectIF(staffid);
        if (staffExVos.isEmpty()){
            return new Result(200, "", null);
        }else{
            for (StaffExVo staffExVo : staffExVos) {
              if (staffExVo.getState()==1) {
                  return new Result(500, "", null);
              }
            }

            return new Result(200, "", null);
        }


    }

    @Override
    public Result add2(StaffExVo staffExVo) {
        Date date = new Date();
        Staff attribute = (Staff)session.getAttribute("staff");
        staffExVo.setApplytime(date);
        staffExVo.setApplyid(attribute.getStaffid());

        Integer integer = staffDao.add2(staffExVo);

        return new Result(200, "添加成功", integer);
    }

    @Override
    public Result dj1(Integer staffid) {
        Integer integer = staffDao.dj1(staffid);
        return new Result(200, "冻结成功", integer);
    }

    @Override
    public Result dj2(Integer staffid) {
        Integer integer = staffDao.dj2(staffid);
        return new Result(200, "解冻成功", integer);
    }

    @Override
    public Result findAllStaffByStaffid() {
        List<Integer> allStaffByStaffid = staffDao.findAllStaffByStaffid();
        return new Result(200, "冻结成功", allStaffByStaffid);
    }

    @Override
    public Result findAllStaff() {
        List<Staff> list = staffDao.selectStaffname();
        return new Result(200,"查询所有非店长员工成功",list);
    }
//
//    @Override
//    public Result insert(Staff staff) {
//        Integer integer = staffDao.insert(staff);
//        return integer;
//    }
}

